-- StoneBox
-- Created by chengb Feb/3/2016
-- 隐蔽的石匣格子

require "game/ui/form/hero/UIPropertyInfo"
require "game/ui/form/dungeon/special/UIStoneBox"

-- 初始化
function initStoneBox(self)
    self.item:removeAllChildren();
    local diaryClassId;

    -- 判断是否显示该格子
    local function isShow()
        -- 隐藏的保险箱
        if self.gridData.type == GRID_TYPE_HIDE_STRONGBOX then
            return true;
        end

        local dbase = DungeonM.queryElement(self.gridData.element, "dbase");
        if type(dbase) == "table" then
            -- 如果配置了需要道具才出现，需要判断一下包裹中是否有相应的道具
            if type(dbase["need_property"]) == "number"  then
                if ItemM.getAmount(ME.user, dbase["need_property"]) > 0 then
                    return true;
                end
            elseif type(dbase["need_property"]) == "table" then
                for _, classId in pairs(dbase["need_property"]) do
                    if ItemM.getAmount(ME.user, classId) > 0 then
                        return true;
                    end
                end
            end
        end
        return false;
    end

    if not isShow() then
        return;
    end

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function bookFun()
        -- 强盗日记按钮回调函数
        local dbase = DungeonM.queryElement(self.gridData.element, "dbase");
        if type(dbase["need_property"]) == "table" then
            for _, classId in pairs(dbase["need_property"]) do
                if ItemM.getAmount(ME.user, classId) > 0 then
                    diaryClassId = classId;
                    break;
                end
            end
        end
        -- 打开强盗日记描述界面
        UIMgr.getCurrentScene():removeFormByName("UIPropertyInfo");
        local uiPropertyInfo = UIPropertyInfo.create(diaryClassId);
        UIMgr.getCurrentScene():addForm(uiPropertyInfo);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 打开隐蔽的石匣界面
            local uiForm = UIStoneBox.create(self.gridData, getLocStr("btn_text_bandit_diary"), bookFun);
            -- 该类型的不显示日记按钮
            if self.gridData.type == GRID_TYPE_HIDE_STRONGBOX then
                uiForm = UIStoneBox.create(self.gridData, getLocStr("btn_text_bandit_diary"));
            end
            UIMgr.getCurrentScene():removeFormByName("UIStoneBox");
            UIMgr.getCurrentScene():addForm(uiForm);

            -- 音效
            AudioM.playFx("ui_open1");
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end
